package com.top.mall.main.goods.adapter

import androidx.appcompat.widget.AppCompatTextView
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.top.mall.R
import com.top.mall.custom.TagTextView
import com.top.mall.entity.CateList

class CateTitleAdapter : BaseQuickAdapter<CateList, BaseViewHolder>(R.layout.item_category_title) {


    var selectedLiveData = MutableLiveData<CateList>()

    var selectedPosition = -1
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
                selectedLiveData.postValue(data[field])
            }
        }

    init {
        setOnItemClickListener { adapter, view, position ->
            selectedPosition = position
        }
    }

    override fun convert(holder: BaseViewHolder, item: CateList) {

//        holder.setText(R.id.tvCategoryTitleLabel, item.label_text)
//            .setGone(R.id.tvCategoryTitleLabel, item.label_text.isNullOrEmpty())

        val labelText = item.label_text
        holder.getView<TagTextView>(R.id.tvCategoryTitleName)
            .setContentAndTag(item.name, if (labelText.isNullOrEmpty()) listOf() else listOf(labelText), item.label_color)

        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}